package com.zhf.service.impl;

import com.zhf.base.StatusCode;
import com.zhf.dao.CourseDao;
import com.zhf.dao.impl.CourseDaoImpl;
import com.zhf.pojo.Course;
import com.zhf.service.CourseService;
import com.zhf.utils.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/10/28 19:16
 */

/**
 * 课程管理模块Service层实现类
 */
public class CourseServiceImpl implements CourseService {

    //创建CourseDao
    CourseDao courseDao = new CourseDaoImpl();

    @Override
    public List<Course> findCourseList() {
        List<Course> courseList = courseDao.findCourseList();
        return courseList;
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {
        List<Course> courseList = courseDao.findByCourseNameAndStatus(courseName, status);
        return courseList;
    }

    @Override
    public String saveCourseSalesInfo(Course course) {
        //补全课程营销信息
        String formart = DateUtils.getDateFormart();
        course.setCreate_time(formart);
        course.setUpdate_time(formart);
        course.setStatus(1);

        //执行插入操作
        int row = courseDao.saveCourseSalesInfo(course);

        if (row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public Course findCourseById(int id) {
        Course course = courseDao.ﬁndCourseById(id);
        return course;
    }

    @Override
    public String updateCourseInfo(Course course) {
        int row = courseDao.updateCourseInfo(course);

        //判断是否修改成功
        if (row > 0) {
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    @Override
    public Map<String, Integer> updateCourseStatus(Course course) {
        int row = courseDao.updateCourseStatus(course);
        Map<String, Integer> map = new HashMap<>();
        if (row > 0) {
            if (course.getStatus() == 0) {
                map.put("status", 0);
            } else {
                map.put("status", 1);
            }
        }
        return map;
    }
}
